/*
 * InputManager.cpp
 *
 *  Created on: Feb 9, 2010
 *      Author: dcrown
 */

#include <windows.h>

#include "InputManager.h"
#include "..\Includes\Common.h"
#include "GameManager.h"
#include "Logger.h"

extern GameManager* gpGM;

void InputManager::Create()
{
	Logger::getInstance().LOG(Logger::DEBUG, "InputManager initialized");
	for (int i = 0; i < 4; i++)
	{
		moveKeysDown[i] = false;
	}
	mouseFired = keyFired = false;

}

void InputManager::ProcessMouseIn(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_LBUTTONDOWN:
			if ((!mouseFired) && (!keyFired))
			{
				mouseFired = true;
				gpGM->PlaneFire();
			}
			break;
		case WM_LBUTTONUP:
			mouseFired = false;
			break;
	}
}

void InputManager::ProcessKeyIn(UINT msg, WPARAM wParam, LPARAM lParam)
{
	static bool fired = false;
	switch (wParam)
	{
		case 'w':
		case 'W':
		case VK_UP:
			moveKeysDown[NORTH] = (msg == WM_KEYDOWN);
			MovePlane();
			break;
		case 'a':
		case 'A':
		case VK_LEFT:
			moveKeysDown[WEST] = (msg == WM_KEYDOWN);
			MovePlane();
			break;
		case 's':
		case 'S':
		case VK_DOWN:
			moveKeysDown[SOUTH] = (msg == WM_KEYDOWN);
			MovePlane();
			break;
		case 'd':
		case 'D':
		case VK_RIGHT:
			moveKeysDown[EAST] = (msg == WM_KEYDOWN);
			MovePlane();
			break;
		case VK_SPACE:
			if ((msg == WM_KEYDOWN) && (!keyFired) && (!mouseFired))
			{
				keyFired = true;
				gpGM->PlaneFire();
			}
			else if (msg == WM_KEYUP)
			{
				keyFired = false;
			}

			break;
	}

}

//this is stupid but will work for now
void InputManager::MovePlane()
{
	if (moveKeysDown[NORTH])
	{
		if (moveKeysDown[WEST])
		{
			gpGM->MovePlane(NORTHWEST);
		}
		else if (moveKeysDown[EAST])
		{
			gpGM->MovePlane(NORTHEAST);
		}
		else
		{
			gpGM->MovePlane(NORTH);
		}
	}
	else if (moveKeysDown[SOUTH])
	{
		if (moveKeysDown[WEST])
		{
			gpGM->MovePlane(SOUTHWEST);
		}
		else if (moveKeysDown[EAST])
		{
			gpGM->MovePlane(SOUTHEAST);
		}
		else
		{
			gpGM->MovePlane(SOUTH);
		}
	}
	else if (moveKeysDown[EAST])
	{
		gpGM->MovePlane(EAST);
	}
	else if (moveKeysDown[WEST])
	{
		gpGM->MovePlane(WEST);
	}
	else
	{
		gpGM->MovePlane(NONE);
	}
}
